package com.commerceerp.system.service.Impl;

import com.commerceerp.system.entity.FxGoodInfo;
import com.commerceerp.system.mapper.FxGoodInfoMapper;
import com.commerceerp.system.service.FxGoodInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.*;

@Service
public class FxGoodInfoServiceImpl implements FxGoodInfoService{

    @Autowired
    private FxGoodInfoMapper fxGoodInfoMapper;


    /**
     * 添加分销商品
     * @param fxGoodInfo
     * @return
     */
    @Override
    public Integer addFxGoodInfo(FxGoodInfo fxGoodInfo) {

        Integer success = this.fxGoodInfoMapper.addFxGoodInfo(fxGoodInfo);

        return success;
    }


    /**
     * 获取待审核分销商品记录
     * @param oneLevelId
     * @param twoLevelId
     * @param goodName
     * @param beginPrice
     * @param endPrice
     * @param beginTime
     * @param endTime
     * @param goodState
     * @param nowSDate
     * @param nowEDate
     * @return
     */
    @Override
    public List<Map> getFxGoodRecordByGoodState(String oneLevelId, String twoLevelId, String goodName, String beginPrice, String endPrice, String beginTime, String endTime, String goodState, String nowSDate, String nowEDate,String historyDate) {

        List<Map> list = this.fxGoodInfoMapper.getFxGoodRecordByGoodState(oneLevelId,twoLevelId,goodName,beginPrice,endPrice,beginTime,endTime,goodState,nowSDate,nowEDate,historyDate);

        return list;
    }


    /**
     * 对待审核的分销商品进行操作
     * @return
     */
    @Override
    public Integer updateFxGoodInfo(FxGoodInfo fxGoodInfo) {

        Integer success = this.fxGoodInfoMapper.upddateFxGoodInfo(fxGoodInfo);

        return success;
    }


    /**
     * 查询客户端所有分销商品
     * @param beginTime
     * @param endTime
     * @param goodName
     * @return
     */
    @Override
    public List<Map> getAllFxGoodInfoByUser(String beginTime, String endTime, String goodName,String loginUserId,String levelName) {

        List<Map> list = this.fxGoodInfoMapper.getAllFxGoodInfoByUser(beginTime,endTime,goodName,loginUserId,levelName);

        return list;
    }

    @Override
    public Integer deleteFxGoodInfo(String goodId) {

        Integer success = this.fxGoodInfoMapper.deleteFxGoodInfo(goodId);

        return success;

    }


    /**
     * 获取分销商品首页商品信息
     * @param goodName
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map> getFxIndexGoodInfo(String goodName,String levelName, String beginTime, String endTime) {

        List<Map> list = this.fxGoodInfoMapper.getFxIndexGoodInfo(goodName,levelName,beginTime,endTime);

        return list;
    }


    /**
     * 获取详细分销商品
     * @param goodId
     * @return
     */
    @Override
    public Map<String, Object> getDetailFxGoodInfoByGoodId(String goodId) {

        Map<String,Object> map = this.fxGoodInfoMapper.getDetailFxGoodInfoByGoodId(goodId);

        String goodColorAndStocks = map.get("goodColorStock").toString();

        String[] goodColorAndStock = goodColorAndStocks.split(",");
        List<String> colorAndStocks = Arrays.asList(goodColorAndStock);

        List<Map> colorAndStockList = new ArrayList<Map>();

        Integer goodAllNum = 0;

        for(String colorAndStock : colorAndStocks){

            Integer index = colorAndStock.indexOf("_");
            String color = colorAndStock.substring(0,index);
            String stock = colorAndStock.substring(index+1,colorAndStock.length());
            Map<String,Object> csMap = new HashMap<String,Object>();
            csMap.put("color",color);
            csMap.put("stock",stock);
            colorAndStockList.add(csMap);

            goodAllNum = goodAllNum + Integer.parseInt(stock);
        }
        map.put("colorAndStocks",colorAndStockList);
        map.put("goodAllNum",goodAllNum);
        map.put("goodAllprice",new BigDecimal(map.get("goodPrice").toString()).multiply(new BigDecimal(goodAllNum.toString())));

        return map;

    }
}
